package com.traffic.util;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.traffic.entity.Bigdata;

import com.traffic.entity.Param;
import com.traffic.service.BigdataService;
import com.traffic.service.ParamService;
import org.apache.ibatis.jdbc.Null;
import org.rosuda.REngine.REXP;
import org.rosuda.REngine.REXPMismatchException;
import org.rosuda.REngine.REngine;

import org.rosuda.REngine.REngineException;
import org.rosuda.REngine.Rserve.RConnection;
import org.rosuda.REngine.Rserve.RserveException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.zip.DeflaterOutputStream;

@Component
public class RUtil {

    @Autowired
    BigdataService bigdataService;

    @Autowired
    ParamService paramService;


    //使用前要开启R语言及其端口
    //library(Rserve);
    //Rserve();

    public Double R_MaxLik(int type) throws RserveException, REXPMismatchException, REngineException {


        List<Bigdata> bg = new ArrayList<>();

        QueryWrapper<Bigdata> wrapper = new QueryWrapper<>();

        bg = bigdataService.list(wrapper.eq("type",type));

        int[] y = new int[bg.size()];
        double[] x1 = new double[bg.size()];
        double[] x2 = new double[bg.size()];
        double[] x3 = new double[bg.size()];
        double[] x4 = new double[bg.size()];
        double[] x5 = new double[bg.size()];
        double[] x6 = new double[bg.size()];
        double[] x7 = new double[bg.size()];
        double[] x8 = new double[bg.size()];

        for (int i=0;i<bg.size();i++) {
            y[i] = bg.get(i).getY();

            x1[i] = bg.get(i).getX1();
            x2[i] = bg.get(i).getX2();
            x3[i] = bg.get(i).getX3();
            x4[i] = bg.get(i).getX4();
            x5[i] = bg.get(i).getX5();
            x6[i] = bg.get(i).getX6();
            x7[i] = bg.get(i).getX7();
            x8[i] = bg.get(i).getX8();
        }

        RConnection rc = new RConnection();

        rc.assign("y",y);

        rc.assign("x1",x1);
        rc.assign("x2",x2);
        rc.assign("x3",x3);
        rc.assign("x4",x4);
        rc.assign("x5",x5);
        rc.assign("x6",x6);
        rc.assign("x7",x7);
        rc.assign("x8",x8);

        rc.eval("df <- data.frame(x1,x2,x3,x4,x5,x6,x7,x8)");
        rc.eval("model <- glm(y~x1+x2+x3+x4+x5+x6+x7+x8,data=df,family = binomial(link ='logit'), na.action(na.omit))");

        REXP rexp=rc.eval("model$coefficients");
        //返回类型是一个双精度数组，所以用asDoubles

      //  System.out.println(rexp.asDoubles()[1]);
        rc.close();//用完记得关闭连接

        double[] parValue = rexp.asDoubles();
        String parList[] = null;
        if (type ==1){
            parList = new String[]{"B1","B2","B3","B4","B5","B6","B7","B8"};
        }else if (type==2){
            parList = new String[]{"B0","B9","B10","B11","B12","B13","B14","B15","B16"};
        }

        for (int i =0;i<8;i++){

            if (i==0 && type ==1){continue;}
            System.out.println(parValue[i]);
            if(Double.isNaN(parValue[i])){
                parValue[i]=0.000001;
            }
            paramService.update(new UpdateWrapper<Param>().set("value",parValue[i]).eq("name",parList[i]));
            System.out.println(parValue[i]);
        }


        return 0.0;
    }

}
